package com.sl.hx.shop.ui.mine.presnter;

import android.content.Context;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.HeadBean;
import com.sl.hx.shop.bean.ImageUrlBean;
import com.sl.hx.shop.bean.LoginBean;
import com.sl.hx.shop.bean.NickName;
import com.sl.hx.shop.constant.AppConfig;
import com.sl.hx.shop.http.ApiFactory;
import com.sl.hx.shop.http.ApiRetrofit;
import com.sl.hx.shop.http.HttpUtil;
import com.sl.hx.shop.http.ProgressSubscribe;
import com.sl.hx.shop.ui.mine.contacts.MineContacts;
import com.sl.hx.shop.ui.mine.contacts.SettingContacts;
import com.sl.hx.shop.utils.HMACSHA1;
import com.sl.hx.shop.utils.SPUtils;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.io.File;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class SettingPresenter extends BasePresenter<SettingContacts.view> implements SettingContacts.presenter {

    private static final String KEY = "shengli";
    private String sign="";
    private Context mContext;

    /** 倒计时 */
    private GetIdeCodeCountDownTimer mCountDownTimer;
    /** 验证码的view */
    private TextView mGetIdeCodeView;
    /** 倒计时是否开启 */
    private boolean mIsTimerStart = false;
    public SettingPresenter(Context context) {
        this.mContext=context;
        mCountDownTimer = new GetIdeCodeCountDownTimer(60000, 1000);

    }



    //获取验证码
    public void getCode(TextView yzm, String mPhone) {
        mGetIdeCodeView = yzm;
        Observable observable = ApiFactory.getInstance().getCode(mPhone, HMACSHA1.md5(KEY+mPhone.substring(0,4)));
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<String>(mContext) {
            @Override
            protected void _onNext(String s) {

            }

            @Override
            public void onComplete() {
                super.onComplete();
                sendSuccess();
            }

            @Override
            protected void _onError(String message) {
                getView().showtoast(message);
                LogUtils.e(message);
            }
        }, false, getView(), ActivityEvent.DESTROY);
    }

    //修改手机号
    public void setPhone(final String mphone, String mYzm) {
        Observable observable = ApiFactory.getInstance().bindPhone(SPUtils.getInstance().getString(AppConfig.ID),mphone,mYzm);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<String>(mContext) {
            @Override
            protected void _onNext(String s) {
                getView().setphoneSuccess(mphone);
            }

            @Override
            protected void _onError(String message) {
                getView().showtoast(message);
            }
        }, true, getView(), ActivityEvent.DESTROY);
    }

    /**
     * 验证码发送成功
     */
    public void sendSuccess() {
        getView().showtoast("验证码已发送");
        setCodeView(false);
        mCountDownTimer.cancel();
        mCountDownTimer.start();
        mIsTimerStart = true;
    }

    private void setCodeView(boolean isAble) {
        if (isAble) {
            mGetIdeCodeView.setEnabled(true);
            mGetIdeCodeView.setTextColor(ContextCompat.getColor(mGetIdeCodeView.getContext(), R.color.yzm));
        }else {
            mGetIdeCodeView.setEnabled(false);
            mGetIdeCodeView.setTextColor(ContextCompat.getColor(mGetIdeCodeView.getContext(), R.color.yzm));
        }

    }

    public void uploadHeadImg(String headImageUrl) {
        File file = new File(headImageUrl);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file) ;
        MultipartBody.Part body = MultipartBody.Part.createFormData("logo", file.getName(), requestBody) ;

        MediaType textType = MediaType.parse("text/plain");
        RequestBody name = RequestBody.create(textType, SPUtils.getInstance().getString(AppConfig.ID));

        Observable observable = ApiFactory.getInstance().headUpload(name,body);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<ImageUrlBean>(mContext) {

            @Override
            protected void _onNext(ImageUrlBean imageUrlBean) {
                getView().changeHeadImgSuccess(imageUrlBean);
            }

            @Override
            protected void _onError(String message) {

            }
        }, false, getView(), ActivityEvent.DESTROY);
    }

    public void getUserInfo() {
        Observable observable = ApiFactory.getInstance().getMyhead(SPUtils.getInstance().getString(AppConfig.ID));
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<HeadBean>(mContext) {
            @Override
            protected void _onNext(HeadBean headBean) {
                getView().ShowUserInfo(headBean);
            }
            @Override
            protected void _onError(String message) {

            }
        }, true, getView(), ActivityEvent.DESTROY);
    }


    private class GetIdeCodeCountDownTimer extends CountDownTimer {

        /**
         * 验证码倒计时
         *
         * @param millisInFuture    总时长
         * @param countDownInterval 间隔多长时间
         */
        public GetIdeCodeCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (mGetIdeCodeView != null) {
                mGetIdeCodeView
                        .setText(String.format(mGetIdeCodeView.getContext().getString(R.string.re_get_identify_code), millisUntilFinished / 1000));
            }
        }

        @Override
        public void onFinish() {
            mIsTimerStart = false;
            setCodeView(true);
            mGetIdeCodeView.setText(mGetIdeCodeView.getContext().getString(R.string.get_identify_code_retry));
        }
    }

    @Override
    public void destroy() {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
    }



}
